<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="70%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="员工姓名" prop="person.name">
                        <el-input v-model="ruleForm.person.name" class="w100p" clearable placeholder="请输入姓名"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="person.gender">
                        <el-select v-model="ruleForm.person.gender" placeholder="请选择性别" class="w100p" clearable>
                            <el-option label="男" value="男"> </el-option>
                            <el-option label="女" value="女"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="身份证">
                        <el-input v-model="ruleForm.person.identificationNumber" class="w100p" clearable
                            placeholder="请输入身份证"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="person.phone">
                        <el-input type="number" v-model="ruleForm.person.phone" class="w100p" clearable
                            placeholder="请输入手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="座机号" prop="person.landline">
                        <el-input v-model="ruleForm.person.landline" class="w100p" clearable
                            placeholder="请输入座机号"></el-input>
                    </el-form-item>
                    <el-form-item label="微信号" prop="person.weChat">
                        <el-input v-model="ruleForm.person.weChat" class="w100p" clearable placeholder="请输入微信"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮箱" prop="person.email">
                        <el-input v-model="ruleForm.person.email" class="w100p" clearable placeholder="请输入邮箱"></el-input>
                    </el-form-item>
                    <el-form-item label="生日" prop="person.birthday">
                        <el-date-picker v-model="ruleForm.person.birthday" class="w100p" type="date"
                            value-format="yyyy-MM-dd" clearable placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="部门" prop="departmentNo">
                        <el-select v-model="ruleForm.departmentNo" placeholder="请选择部门" class="w100p" clearable
                            @change="resetPostNo">
                            <el-option v-for="item, index in departmentList" :key="index" :label="item.departmentName"
                                :value="item.departmentNo">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="岗位" prop="postNo">
                        <el-select v-model="ruleForm.postNo" placeholder="选择岗位前请先选择部门" class="w100p" clearable>
                            <el-option v-if="ruleForm.departmentNo == item.departmentNo" v-for="item, index in stationList"
                                :key="index" :label="item.postName" :value="item.postNo">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="住址" prop="person.address">
                        <el-input type="textarea" :rows="2" v-model="ruleForm.person.address" class="w100p" clearable
                            placeholder="请输入住址"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import departmentManage from '@/api/basicInformationOperate/departmentManage';
import stationManage from '@/api/basicInformationOperate/stationManage';
import staffManage from '@/api/basicInformationOperate/staffManage';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        let validatorIDCard = (rule, value, callback) => {
            if (value) {
                const reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/;
                const card = reg.test(value);
                if (!card) {
                    callback(new Error("身份证号格式有误！"));
                } else {
                    callback();
                }
            } else {
                callback(new Error("请输入身份证号！"));
            }
        };
        return {
            addEditName: '添加',
            addEditApi: staffManage.add,
            ruleForm: {
                person: {}
            },
            departmentList: [],
            stationList: [],
            rules: {
                'person.name': [
                    { required: true, message: '请输入员工姓名', trigger: ['blur', 'change'] }
                ],
                'person.gender': [
                    { required: true, message: '请选择性别', trigger: ['blur', 'change'] }
                ],
                'person.weChat': [
                    { message: '请输入微信号', trigger: ['blur', 'change'] },
                    {
                        pattern: /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/,
                        message: '请输入正确的微信号',
                        trigger: ['blur', 'change']
                    }
                ],
                'person.email': [
                    { message: '请输入邮箱地址', trigger: ['blur', 'change'] },
                    {
                        pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
                        message: '请输入正确的邮箱地址',
                        trigger: ['blur', 'change']
                    }
                ],
                'person.phone': [
                    { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
                    {
                        pattern: /^1[3-9]\d{9}$/, // 手机号正则表达式，这里假设只验证中国大陆手机号  
                        message: '手机号格式不正确',
                        trigger: ['blur', 'change']
                    }
                ],
                'person.identificationNumber': [
                    { required: true, validator: validatorIDCard, trigger: ['blur', 'change'] }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = staffManage.add
                this.addEditName = '添加'
                this.$set(this.ruleForm, 'person', {})
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                !this.ruleForm.person&&this.$set(this.ruleForm, 'person', {})
                this.addEditApi = staffManage.edit
                this.addEditName = '编辑'
            };
            this.initOptionsData();
        },
        async initOptionsData() {
            let { data: departmentList } = await departmentManage.queryByCondition({});
            this.departmentList = departmentList || [];
        },
        async queryPortList(no) {
            let { data: stationList } = await stationManage.queryByCondition({ departmentNo: no });
            this.stationList = stationList || [];
        },
        resetPostNo(no) {
            this.$set(this.ruleForm, 'postNo', '');
            if (no) {
                this.queryPortList(no)
            } else {
                this.stationList = []
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        if (res.code == 1) {
                            this.cancel();
                            this.$message.success(res.msg || '操作成功')
                        } else {
                            this.$message.warning(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$set(this.ruleForm, 'person', {})
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  